<?php
namespace app\index\controller;
use app\index\model\CrmShuju as ThisModel;
use app\index\validate\CrmShuju as ThisValidate;
use think\Exception;

class Shuju extends Base
{
    public $table;
    public function __construct()
    {
        parent::__construct();
        $this->table = new ThisModel();
    }

    // 新增修改
    public function add()
    {
        try {
            $post = input();
            $validate = new ThisValidate();
            if ($validate->check($post)) {
                $post['date'] = strtotime($post['date']);
                if (isset($post['id'])) {
                    if ($this->table->save($post,['id'=>$post['id']])) {
                        apiReturn(200,'修改成功');
                    }else{
                        apiReturn(201,'修改失败');
                    }
                }else{
                    $manageInfo = db('crm_manage')->alias('a')->join('tp_crm_fenzu b','a.zu_id=b.id')->field('a.username,a.name,b.id,b.name as zu_name')->where('a.username',$this->username)->find();
                    if (empty($manageInfo)) {
                        apiReturn(201,'工号不存在');
                    }
                    $post['manage_id'] = $manageInfo['username'];
                    $post['manage_name'] = $manageInfo['name'];
                    $post['zu_id'] = $manageInfo['id'];
                    $post['zu_name'] = $manageInfo['zu_name'];
                    if ($this->table->allowField(true)->save($post)) {
                        apiReturn(200,'新增成功');
                    }else{
                        apiReturn(201,'新增失败');
                    }
                }
            }else{
                apiReturn(201,$validate->getError());
            }
        } catch (Exception $e) {
            apiReturn(201,$e->getMessage());
        }
    }

    // 列表
    public function list()
    {
        $post = input();
        $limit = isset($post['rows']) ? $post['rows'] : 10;
        $page = isset($post['page']) ? $post['page'] : 1;
        $where = [];
        if ($this->is_xiaoshou == 1) {
            $where['manage_id'] = $this->getManageList();
        }
        // 组
        if (isset($post['zu_id'])) {
            if (!empty($post['zu_id'])) {
                $wherezu_id = $this->getFenzuList($post['zu_id']);
                $where['manage_id'] = $this->getManageList($wherezu_id);
            }
        }
        // 工号
        if (isset($post['manage_id'])) {
            if (!empty($post['manage_id'])) {
                $where['manage_id'] = array('in', $post['manage_id']);
            }
        }
        // 时间
        if (isset($post['starttime']) && isset($post['endtime'])) {
            if (!empty($post['starttime']) && !empty($post['endtime'])) {
                $where['date'] = array(array('egt',strtotime($post['starttime'])),array('elt',strtotime($post['endtime'])));
            }
        }
        $list = $this->table->where($where)->order('id DESC')->page($page)->limit($limit)->select();
        $count = $this->table->where($where)->count();
        apiReturn(200,'获取成功',['list'=>$list,'count'=>$count]);
    }

    // 删除
    public function delete()
    {
        $post = input();
        if (!isset($post['id'])) {
            apiReturn(201,'参数不完整');
        }
        if ($this->table->where('id',$post['id'])->delete()) {
            apiReturn(200,'删除成功');
        }else{
            apiReturn(201,'删除失败');
        }
    }

    // 销售统计
    public function shujuManageCount()
    {
        $post = input();//starttime endtime
        $where = [];
        if (isset($post['starttime']) && isset($post['endtime'])) {
            if (!empty($post['starttime']) && !empty($post['endtime'])) {
                $where['date'] = array(array('egt',strtotime($post['starttime'])),array('elt',strtotime($post['endtime'])));
            }else{
                apiReturn(201,'请选择时间');
            }
        }else{
            apiReturn(201,'请选择时间');
        }
        if ($this->is_xiaoshou == 1) {
            $where['manage_id'] = $this->getManageList();
        }
        $list = db('crm_shuju')->where($where)->field('manage_id,manage_name,zu_id,zu_name,sum(customer_num) as customer_num_sum,sum(reply_num) as reply_num_sum,sum(invalid_num) as invalid_num_sum,sum(deal_num) as deal_num_sum,sum(deal_total) as deal_total_sum,sum(baojia_num) as baojia_num_sum,sum(shou_total) as shou_total_sum,sum(delete_num) as delete_num_sum')->group('manage_id')->order('zu_id ASC')->select();
        apiReturn(200,'获取成功',$list);
    }

    // 分组统计
    public function shujuFenzuCount()
    {
        $post = input();//starttime endtime
        $where = [];
        if (isset($post['starttime']) && isset($post['endtime'])) {
            if (!empty($post['starttime']) && !empty($post['endtime'])) {
                $where['date'] = array(array('egt',strtotime($post['starttime'])),array('elt',strtotime($post['endtime'])));
            }else{
                apiReturn(201,'请选择时间');
            }
        }else{
            apiReturn(201,'请选择时间');
        }
        if ($this->is_xiaoshou == 1) {
            $where['manage_id'] = $this->getManageList();
        }
        $list = db('crm_shuju')->where($where)->field('zu_id,zu_name,sum(customer_num) as customer_num_sum,sum(reply_num) as reply_num_sum,sum(invalid_num) as invalid_num_sum,sum(deal_num) as deal_num_sum,sum(deal_total) as deal_total_sum,sum(baojia_num) as baojia_num_sum,sum(shou_total) as shou_total_sum,sum(delete_num) as delete_num_sum')->group('zu_id')->order('zu_id ASC')->select();
        apiReturn(200,'获取成功',$list);
    }

}
